<?php

namespace App\Http\Controllers\Admin\Business\Service;

use App\Http\Controllers\Admin\Business\Repository\Combo as ComboRepository;
use Illuminate\Http\Request;

class Combo
{
    /**
     * 发布状态 1启用 2禁用
     */
    const ON = 1;
    const OFF = 2;
    /**
     * 返利类型
     */
    const RATE_TYPE_SYS = 1;
    const RATE_TYPE_USER = 2;

    /**
     * Http请求
     * @var $request
     */
    private $request;
    private $repository;

    public function __construct ()
    {
        $this->repository = new ComboRepository();
    }
    //常用参数操作
    /**
     * @param $status
     *
     * @return mixed
     */
    public function getStatusString($status){
        $statusList =  $this->getStatusList();
        return $statusList[$status];
    }


    /**
     * @return array
     */
    public static function getStatusList(){
        return [
            self::ON =>  '启用',
            self::OFF =>  '禁用'
        ];
    }

    /**
     * @param $status
     *
     * @return mixed
     */
    public function getRateTypeString($status){
        $statusList =  $this->getRatetypeList();
        return $statusList[$status];
    }


    /**
     * @return array
     */
    public static function getRateTypeList(){
        return [
            self::RATE_TYPE_SYS =>  '根据系统',
            self::RATE_TYPE_USER =>  '自定义'
        ];
    }

    //读操作



    public function getCombo($id){
        return $this->repository->find($id);
    }

    /**
     * @param Request $request
     *
     * @return $this|\Illuminate\Database\Eloquent\Builder|static
     */
    public function getPageList(Request $request,$order=false){
        $this->request = $request;
        $model=$this->repository;
        //关键字
        if(isset($this->request->searchKey)){
            $model=$model->where('title','like', '%'.$this->request->searchKey.'%');
        }

        //状态
        if(isset($this->request->status)&&$this->request->status!=''){
            $model=$model->where('status','=',$this->request->status);
        }
        //返利方式
        if(isset($this->request->rate_types)&&$this->request->rate_types!=''){
            $model=$model->where('rate_type','=',$this->request->rate_types);
        }
        //时间
        if(isset($this->request->searchTime)&&$this->request->searchTime!=''){
            $timeArray=explode('~',$this->request->searchTime);
            $start_time=trim($timeArray[0]);
            $end_time=trim($timeArray[1]);
            $model=$model->whereBetween('created_at',[$start_time,$end_time]);
        }
        if($order==true){
            $model = $model->orderBy('sort','ASC');
        }
        $model=$model->paginate($this->request->limit);

        if($model){
            $model=$model->toArray();
        }
        foreach ($model['data'] as $k=>&$v){
            $v['status_name'] = self::getStatusList()[$v['status']];
            $v['rate_type_name'] = self::getRateTypeList()[$v['rate_type']];

        }
        return $model;
    }

    //写操作
    /**
     * 禁用
     * @param array $id
     */
    public function draft($id = []){
        if($id == []){
            $this->repository->where('status',self::ON)->update(['status'=>self::OFF]);
        }else{
            if(is_array($id)){
                $this->repository->whereIn('id',$id)->where('status',self::ON)->update(['status'=>self::OFF]);
            }else{
                $this->repository->where('id',$id)->where('status',self::ON)->update(['status'=>self::OFF]);

            }
        }
    }

    /**
     * 启用
     * @param array $id
     */
    public function published($id = []){
        if($id == []){
            $this->repository->where('status',self::OFF)->update(['status'=>self::ON]);
        }else{
            if(is_array($id)){
                $this->repository->whereIn('id',$id)->where('status',self::OFF)->update(['status'=>self::ON]);
            }else{
                $this->repository->where('id',$id)->where('status',self::OFF)->update(['status'=>self::ON]);

            }
        }
    }


    public function sortCol($id = [],$sort = 50){
        if($id == []){
        }else{
            if(is_array($id)){
                $this->repository->whereIn('id',$id)->where('sort','<>',$sort)->update(['sort'=>$sort]);
            }else{
                $this->repository->where('id',$id)->where('sort','<>',$sort)->update(['sort'=>$sort]);

            }
        }
    }

    /**
     * @param array $id
     */
    public function del($id = []){
            if($id == []){

            }else{
                if(is_array($id)){
                    $this->repository->whereIn('id',$id)->delete();
                }else{
                    $this->repository->where('id',$id)->delete();

                }
            }
    }

    /**
     * @param $post
     */
    public function insertOrUpdate($post){
        if(isset($post['id'])){
            $model = $this->repository->find($post['id']);
        }else{
            $model = $this->repository;
        }
        $model->title = $post['title'];
        $model->thumb = $post['thumb'];
        $model->price = $post['price']?:0;
        $model->times = $post['times']?:0;
        $model->is_bind=$post['is_bind']?:0;
        $model->zhilv_price=$post['zhilv_price']?:0;
        $model->desc = $post['desc'];
        $model->content = $post['content'];
        $model->rate_type = isset($post['rate_type'])?$post['rate_type']:self::RATE_TYPE_SYS;
        $model->rate = $post['rate']?:0;
        $model->top_rate = $post['top_rate']?:0;
        $model->sort = $post['sort']||$post['sort']===0?$post['sort']:50;
        $model->status = isset($post['status'])?$post['status']:self::OFF;
        $model->save();

    }
}
